package com.dianban.controller.nengliang;

import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.controller.BaseController;
import com.dianban.entity.FacePayCommissionLog;
import com.dianban.entity.User;
import com.dianban.service.FacePayCommissionLogService;
import com.dianban.service.UserService;
import com.dianban.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "ApiFacePayCommissionLogController", description = "佣金明细控制器")
@RestController
@RequestMapping(value = "${interfacePath}/nengliang/commissionLog/")
public class ApiFacePayCommissionLogController extends BaseController {

    @Autowired
    private FacePayCommissionLogService facePayCommissionLogService;
    @Autowired
    private UserService userService;

    @ApiOperation(notes = "list", httpMethod = "GET", value = "佣金明细记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo list(HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        List<FacePayCommissionLog> commissionLogs = new ArrayList<>();
        try {
            commissionLogs = facePayCommissionLogService.findList(user.getId());
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultVo.error(ResultCode.ERROR, "获取失败");
        }
        return ResultVo.success(commissionLogs, "获取成功");
    }

    @ApiOperation(notes = "total", httpMethod = "GET", value = "佣金总金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "total", method = RequestMethod.GET)
    public ResultVo total(HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        Map<String, Object> resultMap = new HashMap<>();
        String total = "0.000";
        try {
            User real = userService.findById(user.getId());
            total = String.valueOf(real.getFacePayCommission().setScale(3, BigDecimal.ROUND_HALF_DOWN));
            resultMap.put("total", total);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultVo.error(ResultCode.ERROR, "获取失败");
        }
        return ResultVo.success(resultMap, "获取成功");
    }
}
